package com.swxc.smartorder.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.swxc.smartorder.event.EventUtil;
import com.swxc.smartorder.event.SimpleEvent;

/**
 * Author   Administrator
 * Date     2018/4/13
 * Desc     eg
 *
 * @author Administrator
 */
public class NetworkChange extends BroadcastReceiver {

    public static final int WITHOUT_NETWORK = 0X0001;
    public static final int WIFI_CONNECT = 0X0010;
    public static final int NETWORK_CONNECT = 0X0011;


    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!networkInfo.isConnected() && !wifiInfo.isConnected()) {


            EventUtil.getInstance().post(new SimpleEvent(SimpleEvent.NETWORK_CHANGE, WITHOUT_NETWORK));

        } else {
            if (wifiInfo.isConnected()) {
                EventUtil.getInstance().post(new SimpleEvent(SimpleEvent.NETWORK_CHANGE, WIFI_CONNECT));


            }
            if (networkInfo.isConnected()) {
                EventUtil.getInstance().post(new SimpleEvent(SimpleEvent.NETWORK_CHANGE, NETWORK_CONNECT));


            }
        }

    }
}